Skip to content

Conversation

FoxxMD
Copy link

@FoxxMD FoxxMD commented Apr 3, 2024

Used fix provided by @jer-sen and closes #582

I believe this only occurs for ESM projects with "moduleResolution": "NodeNext" in tsconfig.json and introduced in TS 4.7. This fix is backwards compatible.

@@ -9,7 +9,7 @@ export { Program, CompilerOptions, Symbol } from "typescript";

const vm = require("vm");

const REGEX_FILE_NAME_OR_SPACE = /(\bimport\(".*?"\)|".*?")\.| /g;
const REGEX_FILE_NAME_OR_SPACE = /(\bimport\(".*?"(, \{ assert: \{ "resolution-mode": "(import|require)" \} \})?\)|".*?")\.| /g;
Copy link

@derdeka derdeka Aug 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @FoxxMD,
thank you for pointing in the right direction. I had to use a different regex when using [email protected] and [email protected] with esm modules:

Suggested change
const REGEX_FILE_NAME_OR_SPACE = /(\bimport\(".*?"(, \{ assert: \{ "resolution-mode": "(import|require)" \} \})?\)|".*?")\.| /g;
const REGEX_FILE_NAME_OR_SPACE = /(\bimport\(.*?\)|".*?")\.| /g;

I'm patching this line with the help of patch-package into typescript-json-schema to make it working again.

@YousefED Thank you for this great package! I would like to politely ask if this regular expression could be updated to work with ESM packages.

edit:
as just discovered @puckey suggests this change:

Suggested change
const REGEX_FILE_NAME_OR_SPACE = /(\bimport\(".*?"(, \{ assert: \{ "resolution-mode": "(import|require)" \} \})?\)|".*?")\.| /g;
const REGEX_FILE_NAME_OR_SPACE =/(\bimport\(".*?"(, [^)]+)?\)|".*?")\.| /g;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

imports with resolution-mode not replaced
2 participants